package cn.iutils.faq.controller;

import cn.iutils.common.Page;
import cn.iutils.common.ResultVo;
import cn.iutils.common.controller.BaseController;
import cn.iutils.common.utils.JStringUtils;
import cn.iutils.faq.entity.Answers;
import cn.iutils.faq.entity.Questions;
import cn.iutils.faq.service.AnswersService;
import cn.iutils.faq.service.QuestionsService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

/**
* 问题表 控制器
* @author iutils.cn
* @version 1.0
*/
@Controller
@RequestMapping("${adminPath}/faq/questions")
public class QuestionsController extends BaseController {

    @Autowired
    private AnswersService answersService;

    @Autowired
    private QuestionsService questionsService;

    @ModelAttribute
    public Questions get(@RequestParam(required = false) String id) {
        Questions entity = null;
        if (JStringUtils.isNotBlank(id)) {
            entity = questionsService.get(id);
        }
        if (entity == null) {
            entity = new Questions();
        }
        return entity;
    }

    /**
     * 问题列表
     * @param model
     * @param page
     * @return
     */
    @RequiresPermissions("faq:questions:view")
    @RequestMapping()
    public String list(Questions questions,Model model, Page<Questions> page) {
        page.setOrderBy("a.create_date desc");
        page.setEntity(questions);
        model.addAttribute("page", page.setList(questionsService.findPage(page)));
        return "faq/questions/list";
    }

    /**
     * 我要提问
     * @param questions
     * @param model
     * @return
     */
    @RequiresPermissions("faq:questions:create")
    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public String create(Questions questions,Model model) {
        model.addAttribute("questions", questions);
        return "faq/questions/form";
    }

    /**
     * 提问
     * @param questions
     * @param redirectAttributes
     * @return
     */
    @RequiresPermissions("faq:questions:create")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public String create(Questions questions, RedirectAttributes redirectAttributes) {
        questionsService.save(questions);
        addMessage(redirectAttributes, "提问成功");
        return "redirect:"+ adminPath +"/faq/questions/create?id="+questions.getId();
    }

    /**
     * 查看问题
     * @param questions
     * @param model
     * @return
     */
    @RequiresPermissions("faq:questions:view")
    @RequestMapping(value = "/view", method = RequestMethod.GET)
    @Transactional
    public String view(Questions questions, Model model,Page<Answers> page) {
        page.setOrderBy("a.create_date desc");
        Answers answers = new Answers();
        answers.setQuestionId(questions.getId());
        page.setEntity(answers);
        page.setList(answersService.findPage(page));
        questions.setPageAnswers(page);
        //查看+1
        questionsService.plusViews(questions.getId());
        model.addAttribute("questions", questions);
        return "faq/questions/view";
    }

    @RequiresPermissions("faq:questions:delete")
    @RequestMapping(value = "/{id}/delete", method = RequestMethod.GET)
    @Transactional
    public String delete(@PathVariable("id") String id,int pageNo,int pageSize, RedirectAttributes redirectAttributes) {
        questionsService.delete(id);
        answersService.deleteByQuestionId(id);
        addMessage(redirectAttributes, "删除成功");
        return "redirect:"+ adminPath +"/faq/questions?pageNo="+pageNo+"&pageSize="+pageSize;
    }

    /**
     * 提交问题
     * @return
     */
    @RequestMapping(value = "/submit", method = RequestMethod.POST)
    public @ResponseBody
    ResultVo submit(Questions questions){
        ResultVo resultVo = null;
        try{
            questionsService.save(questions);
            resultVo = new ResultVo(ResultVo.SUCCESS,"1","调用成功",null);
        }catch (Exception e){
            logger.error("提问接口调用失败",e.getMessage());
            resultVo = new ResultVo(ResultVo.FAILURE,"-1","调用失败",null);
        }
        return resultVo;
    }
}
